package com.mxpio.erp.purchase.controller;


import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.jpa.model.OrderLineKey;
import com.mxpio.erp.common.purchase.entity.PurcInvoiceLine;
import com.mxpio.erp.common.purchase.service.PurcInvoiceLineService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "PurcInvoiceLineController", description = "采购发票行数据接口")
@RestController
@RequestMapping("/erp/purc/pvline/")
public class PurcInvoiceLineController {

    @Autowired
    private PurcInvoiceLineService purcInvoiceLineService;

    @GetMapping("list")
    @Operation(summary = "采购订单行列表", description = "获取采购订单行列表（无分页）", method = "GET")
    public Result<List<PurcInvoiceLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<PurcInvoiceLine> purchaseOrderLines = purcInvoiceLineService.list(PurcInvoiceLine.class, criteria);
        return Result.OK(purchaseOrderLines);
    }

    @GetMapping("page")
    @Operation(summary = "采购订单行列表", description = "获取采购订单行列表（分页）", method = "GET")
    public Result<Page<PurcInvoiceLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<PurcInvoiceLine> purcInvoiceLines = purcInvoiceLineService.listPage(PurcInvoiceLine.class, page, criteria);
        return Result.OK(purcInvoiceLines);
    }

    @GetMapping("list/{bizNo}/{lineNo}")
    @Operation(summary = "根据ID获取采购订单行", description = "根据ID获取采购订单行", method = "GET")
    public Result<PurcInvoiceLine> getById(@Parameter(description="业务单号") @PathVariable(name = "bizNo", required = true) String bizNo, @Parameter(description="行号") @PathVariable(name = "lineNo", required = true)String lineNo) {
        OrderLineKey lineKey = new OrderLineKey();
        lineKey.setLineNo(lineNo);
        lineKey.setBizNo(bizNo);
        PurcInvoiceLine purcInvoiceLine = purcInvoiceLineService.getById(PurcInvoiceLine.class, lineKey);
        return Result.OK(purcInvoiceLine);
    }

    @PostMapping("add")
    @Operation(summary = "新增采购订单行", description = "新增采购订单行", method = "POST")
    public Result<PurcInvoiceLine> add(@RequestBody PurcInvoiceLine purcInvoiceLine) {
        purcInvoiceLineService.save(purcInvoiceLine);
        return Result.OK(purcInvoiceLine);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑采购订单行", description = "编辑采购订单行", method = "PUT")
    public Result<PurcInvoiceLine> edit(@RequestBody PurcInvoiceLine purcInvoiceLine) {
        purcInvoiceLineService.update(purcInvoiceLine);
        return Result.OK(purcInvoiceLine);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除采购订单行", description = "删除采购订单行,多个id用英文逗号分隔,bizNo和lineNo间使用英文冒号分隔,bizNo在前,lineNo在后", method = "DELETE")
    public Result<PurcInvoiceLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            String[] pk = key.split(":");
            OrderLineKey lineKey = new OrderLineKey();
            lineKey.setBizNo(pk[0]);
            lineKey.setLineNo(pk[1]);
            purcInvoiceLineService.remove(PurcInvoiceLine.class, lineKey);
        }
        return Result.OK();
    }
}
